<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-waiting-call-records-widget" :style="getStyle('head')">
      <div class="waiting-inside">
        <!--队列-->
        <div class="column queue">
          <template v-for="item in waitingPatientInfos">
            <div class="queue-item">
              <div class="dept-name">
                <ScrollX>{{ (item.treatmentRoomName) || '\u00a0' }}</ScrollX>
              </div>
              <div class="current-patient">
                <template v-for="(itemInfo) in currentPatientInfos(item.treatingPatient)">
                  <template v-if="!!(itemInfo?.isPause)">
                    <span class="patient-name">
                     {{ (itemInfo.patientName) || '\u00a0' }}
                    </span>
                    <span class="orderNo" v-if="!!(itemInfo?.orderNo)">
                     {{ (itemInfo.orderNo) || '\u00a0' }}
                  </span>
                  </template>
                  <template v-else>
                    {{ '暂停接诊' }}
                  </template>
                </template>
              </div>
            </div>
          </template>
        </div>
        <!--候诊患者-->
        <div class="column wait">
          <div class="wait-inside">
            <div class="header">等待穿刺:{{ waitingPatientNumber.length }}人</div>
            <div class="list-area-wrap">
              <vue-seamless-scroll
                  class="scroll"
                  :ref="`scroll`"
                  :key="widgetKey"
                  :classOption="scrollOption"
                  :data="waitingPatientNumber"
                  v-if="true"
              >
                <div class="list-area">
                  <template v-for="item in waitingPatientSummaryInfos">
                    <div class="group-first">
                      <template v-for="(itemInfo) in item">
                        <div class="patient-info">
                          <div class="patient-info-inside">
                            <span class="patientName">{{ (itemInfo?.patientName) || '\u00a0' }}</span>
                            <span class="orderNo" :class="{'orderNo-no':!(itemInfo?.orderNo) }"
                                  v-if="!!(itemInfo?.orderNo)">
                           {{ (itemInfo?.orderNo) || '\u00a0' }}
                          </span>
                          </div>
                        </div>
                      </template>
                    </div>
                  </template>
                </div>
              </vue-seamless-scroll>
            </div>
          </div>

        </div>
        <!--呼叫记录-->
        <div class="column records">
          <div class="header">已呼叫</div>
          <div class="list-area">
            <template v-for="item in callPatientRecordInfos">
              <div class="group-first">
                <ScrollEditX :content="patientRecords(item) || ''"/>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from 'uuid';
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import ScrollEditX from '@c/public/seamlessScroll/ScrollEditX.vue'
import ScrollEditY from "@c/public/seamlessScroll/ScrollEditY.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

let buildParent = buildParentApi();

export default {
  name: "scene-waiting-call-records-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  components: {
    ScrollEditY,
    ScrollX,
    ScrollEditX,
    CompOperate,
    vueSeamlessScroll,
  },

  data() {
    return {
      waitPatientNumber: 0,
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },

  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "list":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "listItem":
            Reflect.set(Obj, "lineHeight", pxToVw(info.lineHeight));
            Reflect.set(Obj, "height", pxToVw(info.lineHeight));
            return Obj;
        }
      };
    },

    // 滚动相关配置
    scrollOption() {
      const {waitScrollNumber = 5, waitScrollStep = 1, waitScrollState = false} = (this.optionsInfo);
      return {
        hoverStop: true,
        direction: 1,                               // 0向下 1向上 2向左 3向右
        isSingleRemUnit: true,
        step: [1, 1.5, 2, 2.5, 3][waitScrollStep - 1], // 数值越大速度滚动越快
        limitMoveNum: waitScrollState ? waitScrollNumber : 999999, // 开始无缝滚动的数据量 this.dataList.length
      }
    },

    // 列表的队列的患者
    waitingPatientInfos() {
      let devType = (this.devType);
      let {showRowNums} = (this.optionsInfo);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        if (WaitingPatientList.apiData.length > showRowNums) {
          return WaitingPatientList.apiData.slice(0, showRowNums);
        } else {
          let arr = (new Array(showRowNums - WaitingPatientList.apiData.length).fill({}));
          return [...WaitingPatientList.apiData, ...arr];
        }
      } else {
        if (WaitingPatientList.mockData.length > showRowNums) {
          return WaitingPatientList.mockData.slice(0, showRowNums);
        } else {
          let arr = (new Array(showRowNums - WaitingPatientList.mockData.length).fill({}));
          return [...WaitingPatientList.mockData, ...arr];
        }
      }
    },

    // 获取队列正在就诊患者--1位
    currentPatientInfos() {
      return function (row) {
        if (row?.length) {
          return row.slice(0, 1)
        } else {
          return [{}]
        }
      }
    },

    // 获取队列候诊患者并成二维数组
    waitingPatientSummaryInfos() {
      const self = this;
      let chunkOriginal = Array()
      let waitPatientLists = (self.waitingPatientInfos)
      let summary = self.$lodash.map(waitPatientLists, "waitPatientList");
      let flattenArr = self.$lodash.flattenDeep(summary);
      let uniqByArr = self.$lodash.uniqBy(flattenArr, 'queuingId');
      this.waitPatientNumber = uniqByArr.length
      if (uniqByArr.length >= 10) {
        chunkOriginal = [...uniqByArr]
      } else {
        let arr = (new Array(10 - uniqByArr.length).fill({}));
        chunkOriginal = [...uniqByArr, ...arr];
      }
      return self.$lodash.chunk(chunkOriginal, 2)
    },

    // 获取候诊患者数量
    waitingPatientNumber() {
      const self = this;
      let waitingPatientInfos = (self.waitingPatientSummaryInfos);
      let flattenArr = self.$lodash.flattenDeep(waitingPatientInfos);
      let filterArr = self.$lodash.reject(flattenArr, self.$lodash.isEmpty)
      return filterArr || []
    },

    // 列表的队列的就诊
    callPatientRecordInfos() {
      const self = this;
      let list = Array();
      let devType = (self.devType);
      let {recordsNumber} = self.optionsInfo;
      let WaitingPatientList = self.$store.getters['getWaitingPatientBusinessInfo'];
      if (devType) {
        list = (WaitingPatientList.apiData);
      } else {
        list = (WaitingPatientList.mockData);
      }
      if (!!list && !!(list.length)) {
        if ((list.length) > 5) {
          return list.slice(0, recordsNumber);
        } else {
          let arr = (new Array(recordsNumber - list.length).fill({}));
          return [...list, ...arr];
        }
      } else {
        return new Array(recordsNumber).fill({})
      }
    },

    patientRecords() {
      return function (row) {
        let temp;
        let patientNameInfo;
        if (!!(row?.patientName)) {
          patientNameInfo = `<span class="patient-name">${(row?.patientName) || '\u00a0'}</span>`;
        }
        if (!!this.$lodash.trim(patientNameInfo)) {
          if (!!(row?.treatmentRoomName)) {
            temp = `${this.$lodash.trim(patientNameInfo)} <span class="patient-dept">${(row?.treatmentRoomName) || '\u00a0'}</span>`
          } else {
            temp = `${this.$lodash.trim(patientNameInfo)}`
          }
        } else {
          if (!!(row?.treatmentRoomName)) {
            temp = `<span class="patient-dept">${(row?.treatmentRoomName) || '\u00a0'}</span>`
          }
        }
        return temp
      }
    },

    // 就诊患者&暂停接诊
    patientName() {
      return function (row) {
        let isPause = (row?.isPause);
        if (!isPause) {
          return (itemInfo.orderNo) || '\u00a0';
        } else {
          return '暂停接诊';
        }
      }
    },
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 等待候诊滚动结束
    handleScrollEnd(e, row) {
      const self = this;
      if (self.$refs[`scroll-${row.workerId}`]) {
        console.log('等待就诊滚动结束');
        self.$nextTick(() => {
          self.$refs[`scroll-${row.workerId}`][0] && self.$refs[`scroll-${row.workerId}`][0].reset()
        })
      }
    },

    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
          // 队列列表
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          //buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-患者信息', key: key, data: data}));
          await this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
          // 呼叫患者列表
        case "patientRecordsListInfo":
          let patientRecordsList = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setWaitingPatientBusinessInfo', patientRecordsList);
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-waiting-call-records-widget {
  background-color: transparent;
  color: #333;
  align-self: center;
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 800px;
  box-sizing: border-box;
  margin: 20px 144px 10px 144px;
  overflow: hidden;
  backdrop-filter: blur(4px);

  .waiting-inside {
    width: 100%;
    display: flex;
    height: 100%;
    overflow: hidden;
    font-family: 'sharkFitHealth', serif !important;

    .column {
      height: 100%;
      flex: 1;
      margin-right: 40px;
      box-sizing: border-box;
      border-radius: 20px !important;
      font-size: 48px;
      flex-shrink: 0;
      overflow: hidden;

      &:last-child {
        margin-right: 0;
      }
    }

    .queue {
      display: flex;
      flex-direction: column;
      flex-grow: 2.1;
      flex-shrink: 0;

      .queue-item {
        display: flex;
        flex: 1;
        background: rgba(14, 182, 201, 1);
        margin-bottom: 30px;
        overflow: hidden;
        border-radius: 18px;
        padding: 30px 24px;
        box-sizing: border-box;
        flex-direction: column;

        &:last-child {
          margin-bottom: 0;
        }

        .dept-name {
          width: 100%;
          font-size: 120px;
          color: #FFFFFF;
          line-height: 1.4;
          text-align: center;
          box-sizing: border-box;
        }

        .current-patient {
          font-size: 86px;
          color: #14314B;
          text-align: center;
          width: 100%;
          border-radius: 24px;
          line-height: 1.6;
          margin-top: 14px;
          box-sizing: border-box;
          align-self: center;
          align-items: center;
          justify-content: center;
          display: inline-flex;
          background: rgba(255, 244, 159, 1);
        }

        .orderNo {
          overflow: hidden;
          color: #FFFFFF;
          padding: 14px 24px 4px 24px;
          font-size: 74px;
          line-height: 74px;
          background: #14314B;
          margin-left: 20px;
          display: inline-flex;
          border-radius: 14px;
        }
      }
    }

    //候诊
    .wait {
      display: inline-flex;
      flex-direction: column;
      flex-grow: 2.2;
      flex-shrink: 0;
      align-self: start;
      min-width: 600px;

      .wait-inside {
        height: 100%;

        .header {
          width: 100%;
          height: 110px;
          overflow: hidden;
          display: flex;
          flex-direction: column;
          align-items: center;
          align-self: center;
          background: #0EB6C9;
          color: #FFFFFF;
          justify-content: center;
          font-size: 60px;
          border-radius: 24px;
        }

        .list-area-wrap {
          overflow: hidden;
          margin-top: 10px;
          height: calc(100% - 110px);

          .scroll {
            overflow: hidden;
            height: 100%;
            border-radius: 18px;

            :deep(>div) {
              height: auto;
            }
          }
        }

        .list-area {
          display: flex;
          height: calc(100%);
          flex-direction: column;
          overflow: hidden;

          .group-first {
            display: flex;
            background: white;
            margin-top: 12px;
            flex-grow: 1;
            border-radius: 18px;
            overflow: hidden;
            box-sizing: border-box;
            height: calc((100% / 5)) !important;
            min-height: 128px !important;


            &:first-child {
              margin-top: 0;
            }

            &:last-child {
              margin-bottom: 10px;
            }

            .patient-info {
              flex: 1 0 calc(100% / 2);
              padding: 30px 0;
              font-size: 54px;
              color: #14314B;
              min-width: calc(100% / 2);
              max-width: calc(100% / 2);
              box-sizing: border-box;
              display: flex;
              justify-content: center;
              align-items: center;

              .patient-info-inside {
                padding: 0 24px;
                @include singleEllipsis(100%)
              }
            }

            .orderNo {
              overflow: hidden;
              color: #FFFFFF;
              padding: 8px 18px 4px 18px;
              font-size: 44px;
              background: #14314B;
              border-radius: 14px;
              margin-left: 20px;
            }

            .orderNo-no {
              background: #FFFFFF;
            }
          }
        }
      }

    }

    //呼叫记录
    .records {
      flex: 1.2;

      .header {
        width: 100%;
        height: 110px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        align-items: center;
        align-self: center;
        background: rgba(244, 167, 44, 1);
        color: #FFFFFF;
        justify-content: center;
        font-size: 60px;
        border-radius: 24px;
      }

      .list-area {
        display: flex;
        height: calc(100% - 110px);
        flex-direction: column;
        overflow: hidden;

        .group-first {
          display: flex;
          background: white;
          margin-top: 10px;
          flex-grow: 1;
          width: 100%;
          min-height: calc(100% / 5 - 10px);
          box-sizing: border-box;
          border-radius: 18px;
          overflow: hidden;
          justify-content: center;
          align-items: center;
          align-self: center;
        }

        :deep(.st-section) {
          > span {
            &:last-child {
              overflow: hidden;
              color: #FFFFFF;
              padding: 8px 18px 4px 18px;
              font-size: 44px;
              white-space: nowrap;
              border-radius: 14px;
              background: #14314B;
              margin-left: 16px;
            }
          }
        }

        .patient-info {
          white-space: nowrap;
        }

        .dept-name {
          overflow: hidden;
          color: #FFFFFF;
          padding: 8px 18px 4px 18px;
          font-size: 44px;
          white-space: nowrap;
          border-radius: 14px;
          background: #14314B;
          margin-left: 16px;
        }
      }
    }
  }
}
</style>